<template>
  <div class="InvitationCode" :style="'min-height: ' + docheight + 'px'">
    <div v-if="!showTimeOut && showCont">
      <div class="codeUserCont">
        <div class="codeUserTitle">
          <div>接单记录</div>
          <div class="timeBtn">
            <span v-if="startDate && endDate">
              <span @click="showDate = true"
                >{{ startDate }}～{{ endDate }}</span
              >
              <span style="color:#4fc763" @click="czTime">重置日期</span>
            </span>
            <span v-else
              >订单日期:<span @click="showDate = true" style="color:#4fc763"
                >未选择</span
              ></span
            >
          </div>
        </div>
        <div class="codeUserList">
          <div
            class="codeUserItem"
            :key="index"
            v-for="(item, index) in userArr"
          >
            <div class="topCont">
              <img :src="item.package_cover" />
              <div class="userInfo">
                <div class="userName">
                  <span class="desTag" v-if="item.order_status == 2"
                    >待接单</span
                  >
                  <span class="desTag pinkTag" v-if="item.order_status == 3"
                    >待服务</span
                  >
                  <span class="desTag greenTag" v-if="item.order_status == 5"
                    >已完成</span
                  >
                  <span
                    class="desTag blacktag"
                    v-if="item.order_status == 7 && item.refund_type == 1"
                    >拒单</span
                  >
                  <span
                    class="desTag blacktag"
                    v-if="item.order_status == 7 && item.refund_type == 2"
                    >已取消</span
                  >
                  <span>{{ item.package_name }}</span>
                </div>
                <div class="userTime">核销时间：{{ item.check_time }}</div>
                <div class="userTime">预约时间：{{ item.service_time }}</div>
              </div>
            </div>
            <div class="underCont" v-if="item.order_status == 2">
              <div class="orderBtn sureBtn" @click="sureFn(1, item)">
                接单
              </div>
              <div class="orderBtn" @click="sureFn(2, item)">
                拒单
              </div>
            </div>
            <div class="remarkVal">
              备注:{{ item.service_remark ? item.service_remark : "无备注" }}
            </div>
            <div class="remarkVal">
              地址:{{ item.service_address }} {{ item.house_number }}
            </div>
            <div class="remarkVal flexText">
              <div>电话:{{ item.user_phone }}({{ item.user_name }})</div>
              <div
                class="telText"
                @click="telFn(item.user_phone)"
                v-if="item.showPhone"
              >
                拨号
              </div>
            </div>
            <div class="remarkVal" v-if="item.jz_num > 0">
              加钟:{{ item.jz_num }}个钟（合计加钟费：¥{{ item.jz_money }}）
            </div>
          </div>
          <div class="nodataTip" v-if="userArr.length == 0">
            你还没有订单记录哦～
          </div>
          <div class="nodataTip" v-if="userArr.length > 0">
            {{ loadMoreText }}
          </div>
        </div>
      </div>
      <sub-load v-if="showLoad"></sub-load>
    </div>
    <van-calendar
      ref="calend"
      :min-date="minDate"
      :max-date="maxDate"
      color="#4fc763"
      v-model:show="showDate"
      type="range"
      @confirm="onConfirm"
    />
    <toast
      v-model="showToast"
      position="middle"
      width="50%"
      type="text"
      :time="1000"
      >{{ showText }}</toast
    >
    <van-popup v-model:show="showMask" round position="bottom">
      <div class="orderMask">
        <div class="maskTitle">操作提示</div>
        <div class="maskTip">{{ maskTipCont }}</div>
        <saveBtn
          color="#4fc763"
          style="width: 100%;"
          :value="maskBtnCont"
          @saveClick="sureMask"
        ></saveBtn>
      </div>
    </van-popup>
    <footcpm></footcpm>
    <getLoad v-if="!showCont"></getLoad>
    <getTimeOut v-if="showTimeOut"></getTimeOut>
  </div>
</template>

<script>
import { Toast } from "vux";
import SaveBtn from "@/components/public/saveBtn.vue";
import subLoad from "@/components/public/subLoad.vue";
import getLoad from "@/components/public/getLoad";
import getTimeOut from "@/components/public/getTimeOut";
import footcpm from "@/components/footcpm";
import { Receiving, sureOrder, refuseOrder } from "@/http/api";
import bus from "@/assets/evnetBus.js";

export default {
  components: { getTimeOut, getLoad, subLoad, Toast, SaveBtn, footcpm },
  data() {
    return {
      showTimeOut: false,
      showCont: false,
      showLoad: false,
      showText: "",
      loadMoreText: "数据加载中～",
      showToast: false,
      showDate: false,
      userArr: [],
      minDate: new Date(2022, 0, 1),
      maxDate: new Date(),
      startDate: "",
      endDate: "",
      page: 1,
      per_page: 4,
      last_page: 0,
      getting: false,
      showMask: false,
      activeItem: {},
      maskTipCont: "",
      maskBtnCont: "",
      maskType: 1
    };
  },
  props: {},
  methods: {
    getTimeStr(time) {
      let n = time.getFullYear();
      let y = time.getMonth() + 1;
      let d = time.getDate();
      let timeStr = n + "-" + y + "-" + d;
      return timeStr;
    },
    czTime() {
      this.endDate = "";
      this.startDate = "";
      this.page = 1;
      this.last_page = 0;
      this.userArr = [];
      this.$refs.calend.reset();
      this.getData();
    },
    onConfirm(values) {
      const [start, end] = values;
      this.startDate = this.getTimeStr(values[0]);
      this.endDate = this.getTimeStr(values[1]);
      this.showDate = false;
      this.userArr = [];
      this.page = 1;
      this.last_page = 0;
      this.getData();
    },
    telFn(num) {
      window.location.href = "tel:" + num;
    },
    sureMask() {
      if (this.maskType == 1) {
        this.orderFn();
      } else {
        this.cancelFn();
      }
    },
    sureFn(type, item) {
      this.maskType = type;
      if (type == 1) {
        this.maskBtnCont = "同意并接单";
        this.maskTipCont =
          "确认接单之后，请在预约服务时间之前提前到达预约地点并与客户联系,切勿爽约。客户联系电话将在预约服务时间前30分钟显示，请遵守平台服务规则！";
      } else {
        this.maskBtnCont = "确认拒绝此单";
        this.maskTipCont =
          "确认拒绝这一单服务吗？若点击确认拒绝，则会影响技师个人信誉，并且此单将彻底关闭无法再次接受此单！";
      }
      this.showMask = true;
      this.activeItem = item;
    },
    cancelFn() {
      let item = this.activeItem;
      let data = {
        order_id: item.order_id
      };
      this.showMask = false;
      if (!this.showLoad) {
        this.showLoad = true;
        refuseOrder(data)
          .then(res => {
            this.showCont = true;
            if (res.data.code == 1) {
              this.showToast = true;
              this.showText = res.data.msg;
              item.order_status = 7;
              bus.$emit("getWait");
            } else {
              this.showToast = true;
              this.showText = res.data.msg;
            }
            this.showLoad = false;
          })
          .catch(() => {
            this.showTimeOut = true;
            this.showCont = true;
            this.showLoad = false;
          });
      }
    },
    orderFn() {
      let item = this.activeItem;
      let data = {
        order_id: item.order_id
      };
      this.showMask = false;
      if (!this.showLoad) {
        this.showLoad = true;
        sureOrder(data)
          .then(res => {
            this.showCont = true;
            if (res.data.code == 1) {
              this.showToast = true;
              this.showText = res.data.msg;
              item.order_status = 3;
              bus.$emit("getWait");
            } else {
              this.showToast = true;
              this.showText = res.data.msg;
            }
            this.showLoad = false;
          })
          .catch(() => {
            this.showTimeOut = true;
            this.showCont = true;
            this.showLoad = false;
          });
      }
    },
    getData() {
      let time = "";
      if (this.startDate != "" && this.endDate != "") {
        time = this.startDate + "," + this.endDate;
      }
      let data = {
        page: this.page,
        per_page: this.per_page,
        time: time
      };
      Receiving(data)
        .then(res => {
          this.showCont = true;
          if (res.data.code == 1) {
            let arr = res.data.data.data;
            for (let i = 0; i < arr.length; i++) {
              arr[i]["showPhone"] = true;
              if (arr[i].user_phone.indexOf("*") > -1) {
                arr[i]["showPhone"] = false;
              }
              this.userArr.push(arr[i]);
            }
            setTimeout(() => {
              this.getting = false;
            }, 100);
            this.last_page = res.data.data.last_page;
            if (this.page == this.last_page) {
              this.loadMoreText = "到底啦～";
            } else {
              this.loadMoreText = "数据加载中~";
            }
          } else {
            this.showToast = true;
            this.showText = res.data.msg;
          }
        })
        .catch(() => {
          this.showTimeOut = true;
          this.showCont = true;
        });
    },
    nextfn() {
      if (this.$route.path == "/history") {
        let h =
          document.documentElement.scrollHeight || document.body.scrollHeight;
        let wh =
          document.documentElement.clientHeight || document.body.clientHeight;
        let top = document.documentElement.scrollTop || document.body.scrollTop;
        let bottom = h - wh - top;
        if (bottom <= 50) {
          if (this.getting == false) {
            if (this.last_page > this.page) {
              this.getting = true;
              this.page++;
              this.getData();
            }
          }
        }
      }
    }
  },
  mounted() {
    window.addEventListener("scroll", this.nextfn);
  },
  created() {
    this.getData();
  }
};
</script>

<style scoped lang="less">
.InvitationCode {
  width: 100%;
  background-color: #f7f8fa;
  box-sizing: border-box;
  padding-bottom: 3.5rem;
  padding-top: 1rem;
  .codeUserCont {
    width: 92%;
    margin: 0 auto;
    background-color: #fff;
    padding: 0.75rem;
    box-sizing: border-box;
    border-radius: 0.25rem;
    .codeUserTitle {
      font-size: 0.8rem;
      font-weight: bold;
      padding: 0.75rem 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .timeBtn {
        font-weight: normal;
        font-size: 0.65rem;
      }
    }
    .codeUserList {
      padding: 0.75rem 0;
      .codeUserItem {
        width: 100%;
        padding: 0.5rem 0 0.75rem;
        border-bottom: 1px solid #ebebeb;
        .topCont {
          display: flex;
          padding: 0.5rem 0;
          align-items: center;
          img {
            margin-right: 0.75rem;
            width: 3.5rem;
            border-radius: 5px;
          }
          .userInfo {
            width: 2.5rem;
            flex: 1;
            .userName {
              font-size: 0.7rem;
              font-weight: bold;
              color: #333;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              width: 100%;
              .desTag {
                padding: 0.1rem 0.5rem;
                background-color: #4fc763;
                font-size: 0.6rem;
                color: #fff;
                border-radius: 0.1rem;
                font-weight: normal;
              }
              .pinkTag {
                color: #fff;
                background-color: #f56c6c;
              }
              .greenTag {
                background-color: #85ce61;
              }
              .blacktag {
                color: #666;
                background-color: #ebebeb;
              }
            }
            .priceCont {
              align-items: center;
              display: flex;
              font-size: 0.6rem;
              color: #666;
              margin-top: 0.3rem;
              .priceSub {
                margin-left: 0.75rem;
              }
            }
            .userTime {
              font-size: 0.6rem;
              margin-top: 0.25rem;
              color: #666;
            }
          }
        }
        .underCont {
          display: flex;
          align-items: center;
          justify-content: flex-end;
          margin-top: 0.5rem;
          .orderBtn {
            padding: 0.35rem 1.8rem;
            border: 1px solid #4fc763;
            color: #4fc763;
            font-size: 0.6rem;
            margin-left: 0.5rem;
            border-radius: 0.25rem;
          }
          .sureBtn {
            background-color: #4fc763;
            color: white;
          }
        }
        .remarkVal {
          color: #666;
          font-size: 0.6rem;
          margin-top: 0.5rem;
        }
        .flexText {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .telText {
            color: #4fc763;
            text-decoration: underline;
          }
        }
      }
      .nodataTip {
        color: #666;
        font-size: 0.6rem;
        text-align: center;
        margin-top: 20px;
      }
    }
  }
  .orderMask {
    padding: 0.75rem;
    box-sizing: border-box;
    .maskTitle {
      font-size: 0.8rem;
      font-weight: bold;
      text-align: center;
    }
    .maskTip {
      margin-top: 0.5rem;
      line-height: 1.1rem;
    }
  }
}
</style>
<style>
.InvitationCode .van-cell::after {
  border-bottom: none;
}
</style>
